const AdminUser = require('../../model/AdminUser');
const Role = require('../../model/Role');
const Authority = require('../../model/Authority')
const mongoose = require('mongoose')
module.exports = {

    getAllAuthorityByUser: async(adminUser_id) => {
        let adminUser = await AdminUser.findOne({ _id: adminUser_id }, 'role');
        if (!adminUser) {
            throw Error('未查询到此用户')
        }
        let role = adminUser.role;
        let authority = await Role.find({
            _id: { $in: role }
        }, 'authority -_id');
        authority = [] //temp need fix
            // console.log(authority, 'authority')
        let authority_array_string = [];
        authority.forEach(authority_list => {
            authority_list.authority.forEach(authority_item => {
                if (authority_array_string.indexOf(authority_item.toString()) === -1) {
                    authority_array_string.push(authority_item.toString())
                }
            })
        });
        let authority_array = authority_array_string.map(value => mongoose.Types.ObjectId(value));
        let path = await Authority.find({
            _id: { $in: authority_array }
        }, 'path _id');
        let path_array = [];
        path.forEach(path_list => {
            path_list.path.forEach(path_item => {
                if (path_array.indexOf(path_item) === -1) {
                    path_array.push(path_item)
                }
            })
        });
        return JSON.stringify(path_array)
    }
};